package chances.epg.ui.taglib.model.flow;

import java.util.ArrayList;
import java.util.List;



public class FlowLayout {
	
	List<FlowRow> rows ;

	private int lineLength;
	
	private int count;
	
	private FlowRow current;
	
	private int nextBegin;
	

	
	public FlowLayout(int len){
		this.lineLength = len;
		rows = new ArrayList<FlowRow>();
		this.current = initRow();
	}
	
	
	private FlowRow initRow() {
		FlowRow aRowInfo = new FlowRow();
		this.rows.add(aRowInfo);
		this.nextBegin = 0;
		return  aRowInfo;
	}
	
	

	
	public FlowRow addRect(int w ,int space) {
		FlowRow result = this.current;
		this.count++;
		if (this.nextBegin < this.lineLength) {
			nextBegin =nextBegin + w +space;
			this.current.length = nextBegin;
		}else{
			this.current.length = this.current.length-space;
			this.current = initRow();
			this.current.begin = result.begin + result.size;
			nextBegin =nextBegin + w +space;
		}
		this.current.size ++;
		return this.current;
	}
	public List<FlowRow> getRows() {
		return rows;
	}
	
	public int getCount() {
		return count;
	}
	public int getRowCount() {
		return this.rows.size();
	}

	

}
